<!--
描述：题库重复情况组件
开发人：hqj
开发日期：2018年11月15日
-->
<template lang="pug">
    div(v-if="pageShow" style="height:100%;")
        el-form(ref="searchForm" v-bind:model="formModel" v-bind:inline="true" v-on:submit.native.prevent="")
            el-form-item(label="题型" prop="questionType" v-bind:rules="rules.questionType" v-bind:label-width="labelWidth")
                kalix-dict-select(v-model="formModel.questionType" appName="enrolment" dictType="题型" )
            el-form-item(label="去重相似度" prop="similarity" v-bind:rules="rules.similarity" v-bind:label-width="labelWidth")
                el-input(v-model="formModel.similarity")
            el-form-item
                el-button(type="success" v-on:click="onRepeatedClick")
                    i.iconfont.icon-reset
                    | 试题排重
                el-button(type="primary" v-on:click="onQueryClick")
                    i.iconfont.icon-query
                    | 结果查询
        keep-alive
            kalix-table(bizKey="allRepeatedQuery" title='题库重复试题列表' v-bind:targetURL="targetURL"
            v-bind:toolbarBtnList="toolbarBtnList" v-bind:isFixedColumn="isFixedColumn" ref="kalixTable")
                template(slot="tableColumnSlot")
                    el-table-column(type="expand")
                        template(slot-scope="props")
                            el-table( v-bind:data="props.row.repeateList" style="width: 100%; margin-top: -18px; margin-left: 8px;")
                                el-table-column(prop="questionId" label="ID" align="center" width="80px")
                                el-table-column(prop="questionTypeName" label="题型" align="center" width="180px")
                                el-table-column(prop="subTypeName" label="题型子类" align="center" width="180px")
                                el-table-column(prop="stem" label="题干" align="center" width="350px")
                                    template(slot-scope="scope")
                                        el-popover(trigger="hover" placement="top" width="350")
                                            | {{scope.row.stem}}
                                            div(slot="reference")
                                                template(v-if="scope.row.stem && scope.row.stem.length > 20")
                                                    | {{scope.row.stem.substr(0, 20)}}...
                                                template(v-else)
                                                    | {{scope.row.stem}}
                                el-table-column(prop="createBy" label="创建者" align="center" width="120px")
                                el-table-column(prop="creationDate" label="创建时间" align="center" width="180px")
                                    template(slot-scope="scope")
                                        i(class="el-icon-time")
                                        span {{scope.row.creationDate}}
                                el-table-column(prop="checkFlag" label="审核状态" align="center" width="120px")
                                    template(slot-scope="scope")
                                        template(v-if="scope.row.checkFlag === '1'")
                                            el-tag(type="success") 已通过
                                        template(v-else-if="scope.row.checkFlag === '2'")
                                            el-popover(trigger="hover" placement="top" width="120")
                                                | 审核未通过原因
                                                br
                                                | {{scope.row.checkReason}}
                                                div(slot="reference")
                                                    el-tag(type="danger") 未通过
                                        template(v-else)
                                            el-tag 未审核
                                el-table-column(prop="checker" label="审核员" align="center" width="120px")
                                el-table-column(prop="checkDate" label="审核时间" align="center" width="180px")
                                    template(slot-scope="scope")
                                        i(class="el-icon-time")
                                        span {{scope.row.checkDate}}
                                el-table-column(prop="similarity" label="相似度" align="center" width="200px")
                    el-table-column(prop="name" label="重复题目" align="center")
    div(v-else) 题库功能已关闭，如需使用请联系管理员！
</template>

<script type="text/ecmascript-6">
    import FormModel from './model'
    import {QuestionRepeatedURL, QuestionSettingID, QuestionSettingURL, RepeateURL} from '../../config.toml'
    import {questionRepeated} from '../../common.js'

    export default {
        name: 'kalix-enrolment-allrepeatedquery',
        data() {
            var validateSimilarity = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('请输入去重相似度'));
                }
                if (/^(0.\d+|0|1)$/.test(value)) {
                    callback();
                } else {
                    callback(new Error('请输入0～1之间的数字值'));
                }
            }
            return {
                pageShow: false,
                formModel: Object.assign({}, FormModel),
                rules: {
                    questionType: [{required: true, message: '请选择题型', trigger: 'change'}],
                    similarity: [{required: true, validator: validateSimilarity, trigger: 'blur'}]
                },
                toolbarBtnList: [
                    {id: 'add', isShow: false}
                ],
                /*targetURL: RepeateURL.replace('[beans]', 'completions') + '?isAll=true',*/
                targetURL: QuestionRepeatedURL,
                isFixedColumn: true,
                labelWidth: '120px'
            }
        },
        components: {},
        created() {
            /*availableQuestion(this.axios, this.$KalixMessage, flag => {
                this.pageShow = flag
            })*/
            this.pageShow = true
        },
        mounted() {
        },
        activated() {
            // this.$KalixEventBus.$on(this.$KalixEventConfig.ON_SEARCH_BUTTON_CLICK, this.onSearchClick)
        },
        deactivated() {
            // this.$KalixEventBus.$off(this.$KalixEventConfig.ON_SEARCH_BUTTON_CLICK)
        },
        methods: {
            /*onSearchClick(searchObjAll) {
                let beans = 'completions'
                let questionType = JSON.parse(searchObjAll.jsonStr).questionType
                switch (questionType) {
                    case '1' : {
                        beans = 'completions'
                        break
                    }
                    case '2' : {
                        beans = 'choices'
                        break
                    }
                    case '3' : {
                        beans = 'verses'
                        break
                    }
                    case '5' : {
                        beans = 'subjects'
                        break
                    }
                    case '6' : {
                        beans = 'interviewissues'
                        break
                    }
                    default : {
                        beans = 'completions'
                        break
                    }
                }
                this.targetURL = RepeateURL.replace('[beans]', beans) + '?isAll=true'
            }*/
            onRepeatedClick() {
                /*if (!window.questionRepeatedWS) {
                    window.questionRepeatedWS = websocket(this)
                }*/
                if (this.formModel.questionType) {
                    questionRepeated(this.axios, this.$KalixMessage, flag => {
                        if (flag) {
                            this.$KalixMessage.info('试题排重正在执行，请稍候查看结果！')
                            return false
                        } else {
                            let warnInfo = '确定要执行试题排重操作吗?需要等待一段时间,稍后可以查看结果.'
                            this.$confirm(warnInfo, '警告', {
                                confirmButtonText: '确定',
                                cancelButtonText: '取消',
                                type: 'warning'
                            }).then(() => {
                                alert('start')
                            })
                        }
                    })
                } else {
                    this.$KalixMessage.error('请选择题型！')
                    return false
                }
            },
            onQueryClick() {
                this.$refs.searchForm.validate((valid) => {
                    if (valid) {
                        let jsonStr = JSON.stringify({
                            questionType: this.formModel.questionType,
                            similarity: this.formModel.similarity
                        })
                        alert(jsonStr)
                        let searchObjAll = {jsonStr: jsonStr}
                        alert(this.$KalixEventConfig.ON_SEARCH_BUTTON_CLICK)
                        this.$KalixEventBus.$emit(this.$KalixEventConfig.ON_SEARCH_BUTTON_CLICK, searchObjAll)
                        console.log('this.$KalixEventBus', this.$KalixEventBus)
                        console.log('this.$KalixEventConfig.ON_SEARCH_BUTTON_CLICK', this.$KalixEventConfig.ON_SEARCH_BUTTON_CLICK)
                    } else {
                        this.$KalixMessage.error('请检查输入项！')
                        return
                    }
                })
            }
        }
    }
</script>

<style scoped lang="stylus">
</style>
